﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMSLib;
using SMSLib.Gateway;
using SMSLib.Exceptions;
//using System.Windows.Forms;

namespace storeSMS
{
    public static class SMSMessage
    {
        public static SMSLib.Service srv;
        public static void Init()
        {
            try
            {
                if (srv == null)
                {
                    // 初始化.
                    srv = new SMSLib.Service();
                    // 指定发送端口.
                    srv.AddGateway(new ModemGateway("modem.com1", myinfo.com, myinfo.bytl, "Nokia", "6310i", srv.Logger));

                    // 设定端口可以发送短信.
                    srv.FindGateway("modem.com1").Outbound = true;
                    // 设定端口可以接收短信.
                    srv.FindGateway("modem.com1").Inbound = true;

                    srv.FindGateway("modem.com1").inboundMessageHandlers += new AGateway.InboundMessageHandler(InboundMessageNotifier);

                    // 启动服务...
                    srv.StartService();
                }
            }
            catch (SMSLibException e)
            {
                System.Windows.Forms.MessageBox.Show("JDSMSLib Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Generic Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
                

        }
        public static void Send(string sms, string content, int sid)
        {
            if (sms != "" && content != "")
            {
                try
                {
                    Init();
                    // 发送一条短信. 如果发送多条短信，此处可以设定一个循环。
                    OutboundMessage msg = new OutboundMessage(sms, content);

                    // 设置短信编码格式。发送中文一定要设置为UCS2
                    msg.Encoding = SMSLib.Message.MessageEncodings.ENCUCS2;

                    // 设置状态报告
                    msg.StatusReport = true;

                    // 设置短信有效时间（小时）
                    msg.ValidityPeriod = 48;
                    msg.Text = msg.Text.Replace("春笛公司欢迎您!", "");
                    if (srv.SendMessage(msg))
                    {
                        //System.Windows.Forms.MessageBox.Show(msg.ToString());
                        //增加发送信息                    
                        string sendinfo = content;
                        DateTime sendtime = DateTime.Now;
                        string sendnum = sms;
                        Model.sendinfos model = new Model.sendinfos();
                        model.sid = sid;
                        model.sendinfo = sendinfo;
                        model.sendtime = sendtime;
                        model.sendnum = sendnum;
                        BLL.sendinfo bll = new BLL.sendinfo();
                        bll.Add(model);
                        System.Windows.Forms.MessageBox.Show("发送成功！");
                    }
                    // 停止服务
                    //srv.StopService();
                }
                catch (SMSLibException e)
                {
                    System.Windows.Forms.MessageBox.Show("JDSMSLib Exception: " + e.Message);
                    System.Windows.Forms.MessageBox.Show(e.StackTrace);
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("Generic Exception: " + e.Message);
                    System.Windows.Forms.MessageBox.Show(e.StackTrace);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("手机或信息不能为空！");
            }
        }
        public static void Send(string[] smss, string content, int sid)
        {
            try
            {
                Init();
                // 发送一条短信. 如果发送多条短信，此处可以设定一个循环。
                foreach (string sms in smss)
                {
                    OutboundMessage msg = new OutboundMessage(sms, content);

                    // 设置短信编码格式。发送中文一定要设置为UCS2
                    msg.Encoding = SMSLib.Message.MessageEncodings.ENCUCS2;
                    
                    // 设置状态报告
                    msg.StatusReport = true;

                    // 设置短信有效时间（小时）
                    msg.ValidityPeriod = 48;
                    if (srv.SendMessage(msg))
                    {
                        //System.Windows.Forms.MessageBox.Show(msg.ToString());
                        string sendinfo = content;
                        DateTime sendtime = DateTime.Now;
                        string sendnum = sms;
                        Model.sendinfos model = new Model.sendinfos();
                        model.sid = sid;
                        model.sendinfo = sendinfo;
                        model.sendtime = sendtime;
                        model.sendnum = sendnum;
                        BLL.sendinfo bll = new BLL.sendinfo();
                        bll.Add(model); 
                    }
                }
                System.Windows.Forms.MessageBox.Show("发送成功！");
                // 停止服务
                //srv.StopService();
            }
            catch (SMSLibException e)
            {
                System.Windows.Forms.MessageBox.Show("JDSMSLib Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Generic Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
        }

        public static void Read()
        {
            LinkedList<InboundMessage> msgList = new LinkedList<InboundMessage>();

            try
            {
                Init();
                // 读取并显示所有收到的短信.
                srv.ReadMessages(msgList, InboundMessage.MessageClasses.ALL);
                foreach (InboundMessage msg in msgList)
                {
                    //System.Windows.Forms.MessageBox.Show(msg.ToString());
                    string phone ="";
                    string recontent = "";
                    DateTime rtime = DateTime.Now;
                    phone = msg.Originator;
                    if (phone.StartsWith("+86"))
                        phone = phone.Substring(3, phone.Length - 3);
                    recontent = msg.Text;
                    rtime = msg.Date;
                    Model.reinfo model = new Model.reinfo();
                    model.phone = phone;
                    model.recontent = recontent;
                    model.rtime = rtime;
                    BLL.reinfo bll = new BLL.reinfo();
                    bll.Add(model);
                    //生成回复数据
                    BLL.sendinfo bs = new BLL.sendinfo();
                    bs.UpdateR(phone, recontent, rtime);
                    // 删除短信
                    srv.DeleteMessage(msg);
                }
                // 停止服务
                //srv.StopService();
            }
            catch (SMSLibException e)
            {
                System.Windows.Forms.MessageBox.Show("JDSMSLib Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Generic Exception: " + e.Message);
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
        }

        public static void CallNotifier(AGateway gtw, string callerId)
        {
            System.Windows.Forms.MessageBox.Show(string.Format("电话打入: {0}.", callerId));
        }
        public static void InboundMessageNotifier(AGateway gtw, Message.MessageTypes msgType, string memLoc, int memIndex)
        {
            myinfo.f.tsmsg.Text= string.Format("收到短信{0}:{1}.", memLoc, memIndex);
            Read();
        }
        public static void endserveric()
        {
            srv.StopService();
        }

    }
}
